package com.deram.ctin.rest.web.controller.member;

import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.PasswordUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.common.vo.UserVo;
import com.deram.ctin.dependencies.rest.base.BaseController;
import com.deram.ctin.rest.web.result.web.WebMemberInfoResult;
import com.deram.ctin.serv.member.api.MemberInfoApi;
import com.deram.ctin.serv.member.entity.MemberInfo;
import com.deram.ctin.serv.system.api.SystemUserApi;
import com.deram.ctin.serv.system.entity.SystemUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = {"member - 会员接口"})
@RestController
@RequestMapping("/member/user")
public class MemberUserController extends BaseController{

    @Autowired
    private MemberInfoApi memberInfoApi;

    @Autowired
    private SystemUserApi systemUserApi;

    @ApiOperation(value = "个人中心（包括修改密码）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgUrl", value = "头像地址", required = false, paramType = "form"),
            @ApiImplicitParam(name = "nickName", value = "昵称", required = false, paramType = "form"),
            @ApiImplicitParam(name = "phoneNumber", value = "手机号", required = false, paramType = "form"),
            @ApiImplicitParam(name = "oldPassword", value = "老密码", required = false, paramType = "form"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = false, paramType = "form"),
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = false, paramType = "form"),
    })
    @PostMapping("/updateMember")
    public void updateMember( String imgUrl, String nickName, String phoneNumber, String email, String oldPassword, String newPassword) {
        ValidateUtils.isMobile(phoneNumber,false);
        UserVo userVo = super.get();
        MemberInfo memberInfo = memberInfoApi.find(userVo.getId());
        SystemUser systemUser = systemUserApi.find(userVo.getId());
        if(oldPassword != ""){
            ValidateUtils.isPassword(oldPassword, "老密码");
            if (!PasswordUtils.isEqual(oldPassword, systemUser.getPassword(), systemUser.getSalt())) {
                BusinessUtils.throwException("老密码不正确，请重新输入");
            }
        }

        memberInfo.setEmail(email);
        memberInfo.setImageUrl(imgUrl);
        memberInfo.setNickname(nickName);
        if(newPassword != ""){
            systemUser.setPassword(newPassword);
            systemUserApi.updatePassword(userVo.getId(),newPassword);
        }
        memberInfoApi.save(memberInfo);
    }

    @ApiOperation(value = "设置内获取个人信息")
    @GetMapping("/getMember")
    public WebMemberInfoResult getMember(){
        UserVo userVo = super.get();
        MemberInfo memberInfo = memberInfoApi.find(userVo.getId());
        SystemUser systemUser = systemUserApi.find(userVo.getId());
        WebMemberInfoResult webMemberInfoResult = new WebMemberInfoResult();
        webMemberInfoResult.setMemberInfo(memberInfo);
        webMemberInfoResult.setSystemUser(systemUser);
        return webMemberInfoResult;
    }

}